package com.ibiz.workflow.config;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.flowable.bpmn.model.BaseElement;
import org.flowable.bpmn.model.CustomProperty;
import org.flowable.bpmn.model.ExtensionAttribute;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.SequenceFlow;
import org.flowable.editor.language.json.converter.BaseBpmnJsonConverter;
import org.flowable.editor.language.json.converter.SequenceFlowJsonConverter;

import com.fasterxml.jackson.databind.JsonNode;

public class CustomerSequenceFlowJsonConverter extends SequenceFlowJsonConverter {

	public static void fillTypes(Map<String, Class<? extends BaseBpmnJsonConverter>> convertersToBpmnMap,
			Map<Class<? extends BaseElement>, Class<? extends BaseBpmnJsonConverter>> convertersToJsonMap) {
		fillJsonTypes(convertersToBpmnMap);
		fillBpmnTypes(convertersToJsonMap);
	}
	
	public static void fillJsonTypes(Map<String, Class<? extends BaseBpmnJsonConverter>> convertersToBpmnMap) {
        convertersToBpmnMap.put(STENCIL_SEQUENCE_FLOW, CustomerSequenceFlowJsonConverter.class);
    }

    public static void fillBpmnTypes(Map<Class<? extends BaseElement>, Class<? extends BaseBpmnJsonConverter>> convertersToJsonMap) {
        convertersToJsonMap.put(SequenceFlow.class, CustomerSequenceFlowJsonConverter.class);
    }

	@Override
	protected FlowElement convertJsonToElement(JsonNode elementNode, JsonNode modelNode,
			Map<String, JsonNode> shapeMap) {
		// TODO Auto-generated method stub
		FlowElement flow = super.convertJsonToElement(elementNode, modelNode, shapeMap);

		
		String approvalOperation = getPropertyValueAsString("ibiz_approvaloperation", elementNode);
		String approvalMobOperation = getPropertyValueAsString("ibiz_approvalmoboperation", elementNode);
		
		ExtensionAttribute approvalOperationAttribute = new ExtensionAttribute() ;
		approvalOperationAttribute.setName("flowable:approvalOperation");
		approvalOperationAttribute.setValue(approvalOperation);
		
		ExtensionAttribute approvalMobOperationAttribute = new ExtensionAttribute() ;
		approvalMobOperationAttribute.setName("flowable:approvalMobOperation");
		approvalMobOperationAttribute.setValue(approvalMobOperation);

		
		

		Map<String, List<ExtensionAttribute>> atts =new HashMap<String, List<ExtensionAttribute>>();
		atts.put("ibiz_Operation",Arrays.asList(approvalOperationAttribute,approvalMobOperationAttribute));
		flow.setAttributes(atts);
		
		////////////////////
//		ExtensionElement element = new ExtensionElement() ;
//		element.setName("ibizpage");
//		element.addAttribute(iBizPageAttribute);
//		flow.addExtensionElement(element);
		
		return flow;
	}

	private CustomProperty createProperty(String propertyName, String propertyValue) {
        CustomProperty customProperty = new CustomProperty();
        customProperty.setId(propertyName);
        customProperty.setName(propertyName);
        customProperty.setSimpleValue(propertyValue);
        return customProperty;
    }
}

